package com.ljw.gulimall.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ljw.gulimall.member.dao.MemberLevelDao;
import com.ljw.gulimall.member.entity.MemberLevelEntity;
import com.ljw.gulimall.member.exception.PhoneExistException;
import com.ljw.gulimall.member.exception.UsernameExistException;
import com.ljw.gulimall.member.vo.MemberLoginVo;
import com.ljw.gulimall.member.vo.MemberLoginWxVo;
import com.ljw.gulimall.member.vo.MemberRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljw.common.utils.PageUtils;
import com.ljw.common.utils.Query;

import com.ljw.gulimall.member.dao.MemberDao;
import com.ljw.gulimall.member.entity.MemberEntity;
import com.ljw.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    private MemberLevelDao memberLevelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(MemberRegistVo memberRegistVo) {
        MemberEntity memberEntity = new MemberEntity();
        //设置默认等级
        MemberLevelEntity levelEntity = memberLevelDao.getDefaultLevel();
        memberEntity.setLevelId(levelEntity.getId());

        //检查用户名和手机号是否唯一
        checkPhoneUnique(memberRegistVo.getPhone());
        checkUsernameUnique(memberRegistVo.getUserName());

        //设计手机号和用户名
        memberEntity.setMobile(memberRegistVo.getPhone());
        memberEntity.setUsername(memberRegistVo.getUserName());

        //对密码进行加密
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(memberRegistVo.getPassword());
        memberEntity.setPassword(encode);

        //其他的默认信息
        baseMapper.insert(memberEntity);

    }

    @Override
    public void checkPhoneUnique(String phone) throws PhoneExistException{
        LambdaQueryWrapper<MemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberEntity::getMobile, phone);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0){
            throw new PhoneExistException();
        }
    }


    @Override
    public void checkUsernameUnique(String username) throws UsernameExistException{

        LambdaQueryWrapper<MemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberEntity::getUsername, username);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0){
            throw new UsernameExistException();
        }

    }

    @Override
    public MemberEntity login(MemberLoginVo memberLoginVo) {

        String loginacct = memberLoginVo.getLoginacct();
        String password = memberLoginVo.getPassword();

        //去数据库查询用户信息
        LambdaQueryWrapper<MemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberEntity::getUsername, loginacct).or().eq(MemberEntity::getMobile, loginacct);
        MemberEntity memberEntity = baseMapper.selectOne(wrapper);

        //不存在该用户，登入失败
        if (memberEntity == null){
            return null;
        }
        else { //存在该用户，对比密码是否匹配
            String passwordDB = memberEntity.getPassword();
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            boolean matches = encoder.matches(password, passwordDB);
            //匹配成功
            if (matches){
                return memberEntity;
            }
            else {
                return null;
            }
        }
    }

    /**
     * 微信登入和注册
     * @param memberLoginWxVo
     * @return
     */
    @Override
    public MemberEntity wxLogin(MemberLoginWxVo memberLoginWxVo) {

        //查询该微信用户是否已经注册
        String wxId = memberLoginWxVo.getSocialWxId();
        LambdaQueryWrapper<MemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberEntity::getSocialWxId, wxId);
        MemberEntity memberEntity = baseMapper.selectOne(wrapper);
        if (memberEntity == null){
            //这个用户未注册
            MemberEntity regist = new MemberEntity();
            regist.setUsername(memberLoginWxVo.getUsername());
            regist.setSocialWxId(wxId);
            regist.setCity(memberLoginWxVo.getCity());
            regist.setHeader(memberLoginWxVo.getHeader());
            baseMapper.insert(regist);
            return regist;
        }
        return memberEntity;
    }


}